#include <bits/stdc++.h>
using namespace std;
inline long long read(){
	long long c=1,f=0;char ch=getchar();
	while(ch<'0'||ch>'9'){
		ch=getchar();
		if(ch=='-'){
			c=-1;
		}
	}
	while(ch>='0'&&ch<='9'){
		f=f*10+ch-'0';
		ch=getchar();
	}
	return c*f;
}
long long t,n,m,x[8005],y[8005],a[8005],ans;
long long solve(long long t1,long long t2,long long t3){
	if(t1<=t2&&t2<=t3){
		return t2;
	}
	if(t1>=t2&&t2>=t3){
		return t2;
	}
	if(t1>=t3&&t3>=t2){
		return t3;
	}
	if(t1<=t3&&t3<=t2){
		return t3;
	}
	return t1;
}
bool check(long long p){
	if(p%20==3||p<=10){
		for(int i=2;i*i<=p;i++){
			if(p%i==0){
				return false;
			}
		}
		return true;
	}
	return false;
}
int main() {
	freopen("chess.in","r",stdin);
	freopen("chess.out","w",stdout);
	t=read();
	while(t--){
		ans=0;
		n=read();m=read();
		for(int i=1;i<=n;i++){
			x[i]=read();y[i]=read();
		}
		for(int i=1;i<=n;i++){
			a[i]=read();
		}
		for(int i=1;i<=n;i++){
			for(int j=i+1;j<=n;j++){
				for(int k=j+1;k<=n;k++){
					long long temp1=abs(x[i]-x[j])+abs(y[i]-y[j]);
					long long temp2=abs(x[i]-x[k])+abs(y[i]-y[k]);
					long long temp3=abs(x[k]-x[j])+abs(y[k]-y[j]);
					long long res=solve(temp1,temp2,temp3);
					if(check(res)){
						ans+=(a[i]+a[j]+a[k]);
					}
				}
			}
		}
		printf("%lld\n",ans);
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}
